import { addClass, removeClass } from "@/assets/js/dom";
import { mapGetters, mapActions } from "vuex";
import Divider from "vux/src/components/divider/index.vue";
import CheckIcon from "vux/src/components/check-icon/index.vue";

import { setTimeout } from "timers";
import template from "./loginTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    Divider,
    CheckIcon,
  },
  render: render,
  data() {
    return {
      flags: "",

      loginUserName: "",
      loginPsd: "",

      checkedTrue: true, //记住账号
      automaticLog: true, //自动登录
      rememberPass: true, //记住密码

      btnLoading: false,
      info: [],
    };
  },
  created() {
    if (localStorage.getItem("automaticLog")) {
      //自动登录
      this.automaticLog = localStorage.getItem("automaticLog");
      if (this.automaticLog === "true") {
        this.automaticLog = true;
      } else {
        this.automaticLog = false;
      }
    }
    if (localStorage.getItem("userName")) {
      //记住密码
      this.rememberPass = true;
      this.loginUserName = localStorage.getItem("userName");
      this.loginPsd = localStorage.getItem("password");
    }
  },
  mounted() {
    this.getHelp();
  },
  computed: {
    ...mapGetters(["platformConfig", "service"]),
  },
  methods: {
    ...mapActions(["SET_USER_INFO_DATA", "SET_ACCOUNT_DATA"]),
    showHelp(status) {
      if (status == 3) {
        var value = this.info[2].value;
        if (value == "暂无") {
          this.$vux.toast.text("暂无客服", "bottom");
        } else {
          window.location.href = this.info[2].value;
        }
      } else if (status == 2) {
        this.$vux.alert.show({
          content:
            "客服QQ：" +
            (this.info[1].value ? this.info[1].value : "暂无客服QQ"),
        });
      } else if (status == 1) {
        this.$vux.alert.show({
          content:
            "客服微信：" +
            (this.info[0].value ? this.info[0].value : "暂无客服微信"),
        });
      }
    },
    getHelp() {
      this.$http
        .post(this.urlRequest + "?m=api&c=app&a=customerType")
        .then((res) => {
          if (res.status == 0) {
            this.info = res.info;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    eyeOnOff(e) {
      var el = e.currentTarget;
      var type = this.$refs.password.$el.getElementsByTagName("input")[0];

      if (type.type == "password") {
        addClass(el, "eyeOn");
        removeClass(el, "eyeOff");
        type.type = "text";
      } else {
        addClass(el, "eyeOff");
        removeClass(el, "eyeOn");
        type.type = "password";
      }
    },
    mydemo() {
      // 游客
      let params = {
        flag: "3",
        code: "123",
      };
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=user&a=registerMachine", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            localStorage.setItem("token", res.token);
            localStorage.setItem("isUser", false);

            this.$http
              .post(this.urlRequest + "?m=api&c=user&a=userInfo", {
                token: res.token,
              })
              .then((res) => {
                this.SET_USER_INFO_DATA({
                  Obj: res.data,
                });
              });
            this.SET_ACCOUNT_DATA({
              Obj: {},
            });
            this.$vux.toast.show({
              text: "登录成功！",
            });
            this.$router.push({
              //跳转到首页
              path: "/home",
              name: "Home",
            });
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    loginNow() {
      //立即登录
      var params = {
        username: this.loginUserName,
        password: this.loginPsd,
        flag: "3",
        code: "1234",
        type: "1",
        openid: "",
        nickname: "",
        avatar: "",
      };
      if (this.loginUserName == "") {
        this.$vux.toast.show({
          text: "账号不能为空",
        });
        return;
      }

      if (this.loginPsd == "") {
        this.$vux.toast.show({
          text: "密码不能为空",
        });
        return;
      }

      this.btnLoading = true;
      this.$http
        .post(this.urlRequest + "?m=api&c=user&a=login", params)
        .then((res) => {
          this.btnLoading = false;
          if (res.status == 0) {
            localStorage.setItem("token", res.token);
            localStorage.setItem("isUser", true);
            localStorage.setItem("isLogin", "1");

            this.$http
              .post(this.urlRequest + "?m=api&c=user&a=userInfo", {
                token: res.token,
              })
              .then((res) => {
                this.SET_USER_INFO_DATA({
                  Obj: res.data,
                });
              });
            this.SET_ACCOUNT_DATA({
              Obj: {},
            });
            if (this.rememberPass) {
              localStorage.setItem("userName", this.loginUserName);
              localStorage.setItem("password", this.loginPsd);
            } else {
              localStorage.removeItem("userName");
              localStorage.removeItem("password");
            }
            if (this.automaticLog) {
              localStorage.setItem("automaticLog", this.automaticLog);
            }
            this.$vux.toast.show({
              text: "登录成功！",
            });
            this.$router.push({
              path: "/home",
              name: "Home",
            });
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.btnLoading = false;
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    goRegister() {
      //跳转到注册页
      this.$router.push({ path: "/reg" });
    },
  },
  watch: {
    automaticLog(bool) {
      //自动登录
      localStorage.setItem("automaticLog", bool);
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/login/login.vue
